package com.chinar.funds;

import static com.chinar.funds.until.PercentageParser.parsePercentage;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.text.ParseException;
import java.util.List;

public class FundAdapter extends RecyclerView.Adapter<FundAdapter.FundViewHolder> {
    private List<com.chinar.funds.Fund> fundList;
    private com.chinar.funds.onViewClickListener listener;


    public FundAdapter(List<com.chinar.funds.Fund> fundList, com.chinar.funds.onViewClickListener listener) {
        this.fundList = fundList;
        this.listener = listener;
    }

    @NonNull
    @Override
    public FundViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fund, parent, false);
        return new FundViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FundViewHolder holder, int position) {
        // 检查列表是否为空，避免索引越界
        if (fundList.isEmpty()) {
            return;
        }
        
        com.chinar.funds.Fund fund = fundList.get(position);
        holder.tvCode.setText(fund.getCode());
        holder.tvName.setText(fund.getName());
        holder.tvEstimate.setText(fund.getEstimate());
        holder.tvYesterday.setText(fund.getYesterdayNetValue());


            double estimate= Double.parseDouble(fund.getEstimate());
            double yesterdayNetValue= Double.parseDouble(fund.getYesterdayNetValue());
            if (estimate>yesterdayNetValue){
                holder.tvEstimate.setTextColor(0xFFFF0000);
            }else if(estimate<yesterdayNetValue){
                holder.tvEstimate.setTextColor(0xFF00FF00);
            }else {
                holder.tvEstimate.setTextColor(0xFF000000);
            }


        // 点击整个item查看详细信息
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onClick(position);
            }
        });

        // 点击删除按钮删除基金
        holder.btnDelete.setOnClickListener(v -> {
            if (listener != null) {
                // 使用当前列表中的实际位置
                int actualPosition = holder.getAdapterPosition();
                if (actualPosition != RecyclerView.NO_POSITION) {
                    listener.onDeleteClick(actualPosition);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return fundList.size();
    }

    public void updateData(List<com.chinar.funds.Fund> newFundList) {
        this.fundList = newFundList;
        notifyDataSetChanged();
    }

    static class FundViewHolder extends RecyclerView.ViewHolder {
        TextView tvCode, tvName, tvEstimate, tvYesterday;
        Button btnDelete;

        public FundViewHolder(@NonNull View itemView) {
            super(itemView);
            tvCode = itemView.findViewById(R.id.tv_code);
            tvName = itemView.findViewById(R.id.tv_name);
            tvEstimate = itemView.findViewById(R.id.tv_estimate);
            tvYesterday = itemView.findViewById(R.id.tv_yesterday);
            btnDelete = itemView.findViewById(R.id.item_delete_button);
        }
    }
}